package server;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.SocketException;
import java.nio.file.Files;

/**
 * @file Server_Response.java
 * @version 0.1
 * @author James Dibble 10009689
 * 
 * Latest Source:
 * http://code.google.com/p/csp-server/source/browse/
 *                                  trunk/Server/src/server/Server_Response.java
 */
public class Server_Response {
    // <editor-fold defaultstate="collapsed" desc="Attributes">
    private Socket sSocket;
    private PrintWriter pwClientOutput;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Constructors">
    public Server_Response(Socket sSocket) throws IOException {
        pwClientOutput = new PrintWriter(sSocket.getOutputStream());
        this.sSocket = sSocket;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Methods">
    /**
     * Transmit a textual message to the client
     *
     * @param message String to send.
     */
    public void SendToClient(String message) {
        if (Server.bolServerDebug) {
            System.out.println(String.format("Server->%s(%s): %s",
                    sSocket.getInetAddress().getHostName(),
                    sSocket.getInetAddress().toString(),
                    message));
        }
        pwClientOutput.println(message);
        pwClientOutput.flush();
    }
    
    /**
     * Transmit a textual message to the client
     *
     * @param message The predefined message to send.
     */
    public void SendToClient(Client_Messages message) {
        if (Server.bolServerDebug) {
            System.out.println(String.format("Server->%s(%s): %s",
                    sSocket.getInetAddress().getHostName(),
                    sSocket.getInetAddress().toString(),
                    message));
        }
        pwClientOutput.println(message);
        pwClientOutput.flush();
    }

    /**
     * Transmit a file to the client.
     *
     * @param thisResource The server representation of the file.
     * @throws SocketException
     * @throws FileNotFoundException
     * @throws IOException
     */
    public void SendResourceToClient(Resource thisResource) 
            throws SocketException, FileNotFoundException, IOException {
        //Send to the client the size of the file to read and the name of the
        //file to create.
        this.SendToClient(Server_Response.Client_Messages.SENDING.toString() 
                          + (int) thisResource.getResourceAsFile().length()
                          );
        this.SendToClient(Server_Response.Client_Messages.SENDING.toString() 
                          + thisResource.getDownloadFileName()
                          );
        
        //Create a byte array the same size as the file.
        byte[] byteBuff = 
                      new byte[(int) thisResource.getResourceAsFile().length()];
        
        //Copy the contents of the file into the byte array.
        byteBuff = 
                  Files.readAllBytes(thisResource.getResourceAsFile().toPath());
        
        //Clear the sockets buffer and then send the file's contents.
        sSocket.getOutputStream().flush();
        sSocket.getOutputStream().write(byteBuff);
        sSocket.getOutputStream().flush();
        if (Server.bolServerDebug) {
            System.out.println(String.format("Server->%s(%s): %s",
                               sSocket.getInetAddress().getHostName(),
                               sSocket.getInetAddress().toString(),
                               "File uploaded.")
                               );
        }
    }
    // </editor-fold>
    
    /**
     * Standard set of messages to be sent to the client.
     */
    public enum Client_Messages {

        TERMS_OF_SERVICE {

            @Override
            public String toString() {
                return "Terms of reference. Do you accept? Y or N";
            }
        },
        ANOTHER {

            @Override
            public String toString() {
                return "Another? Y or N";
            }
        },
        INVALID1 {

            @Override
            public String toString() {
                return "Invalid input --- ";
            }
        },
        INVALID2 {

            @Override
            public String toString() {
                return "Invalid choice --- ";
            }
        },
        SENDING {

            @Override
            public String toString() {
                return "FILE_INCOMING:";
            }
        };
    }
}
